package com.yq.backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yq.backend.dto.CardGameDto;
import com.yq.backend.mapper.CardGameMapper;
import com.yq.backend.mapper.CardGameProductMapper;
import com.yq.backend.mapper.CardGameRulesMapper;
import com.yq.backend.pojo.CardGame;
import com.yq.backend.pojo.CardGameProduct;
import com.yq.backend.pojo.CardGameRules;
import com.yq.backend.pojo.Result;
import com.yq.backend.service.CardGameService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @descriptions: 奖品管理
 * @author: yunqiang
 * @date: 2022/5/3 11:27
 */
@Service
@Slf4j
public class CardGameServiceImpl implements CardGameService {


    @Autowired
    private CardGameMapper cardGameMapper;
    @Autowired
    private CardGameProductMapper cardGameProductMapper;
    @Autowired
    private CardGameRulesMapper cardGameRulesMapper;


    /**
     * 根据id查询活动
     *
     * @param id
     * @return
     */
    @Override
    public Result getCardGameId(Integer id) {
        CardGame cardGame = cardGameMapper.selectById(id);
        return cardGame == null ? Result.noResult() : Result.success(cardGame);
    }

    /**
     * 删除活动
     *
     * @param id
     * @return
     */
    @Override
    public Result delCardGameById(Integer id) {
        cardGameMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 活动列表
     *
     * @param cardGameDto
     * @return
     */
    @Override
    public Result getCardGames(CardGameDto cardGameDto) {
        int pageNow = cardGameDto.getPageNow();
        int pageSize = cardGameDto.getPageSize();
        if (pageNow > 0){
            pageNow  =(pageNow - 1)*pageSize;
        }else{
            pageNow = 0;
        }
        long total = cardGameMapper.getCardGamesCount(cardGameDto);
        List<CardGame> cardGames = cardGameMapper.getCardGames(cardGameDto,pageNow,pageSize);
        if(CollectionUtils.isEmpty(cardGames)){
           return Result.noResult();
        }
        return Result.success(total,cardGames);
    }

    /**
     * 添加活动
     *
     * @param cardGame
     * @return
     */
    @Override
    public Result addCardGame(CardGame cardGame) {
        List<CardGame> cardGames = cardGameMapper.selectList(new QueryWrapper<CardGame>().lambda().eq(CardGame::getTitle, cardGame.getTitle()));
        if (cardGames.size() > 0) {
            return Result.error(505, "活动名称已存在！");
        }
        cardGameMapper.insert(cardGame);
        return Result.success();
    }

    /**
     * 修改活动
     *
     * @param cardGame
     * @return
     */
    @Override
    public Result upCardGame(CardGame cardGame) {
        cardGameMapper.updateById(cardGame);
        return Result.success();
    }

    /**
     * 奖品配置列表
     * @return
     */
    @Override
    public Result getPrizeAllocations() {
        List<CardGameProduct> cardGameProducts = this.cardGameProductMapper.selectList(null);
        if(CollectionUtils.isEmpty(cardGameProducts)){
            return Result.noResult();
        }

        return Result.success(cardGameProducts);
    }

    /**
     * 添加奖品
     * @param cardGameProduct
     * @return
     */
    @Override
    public Result addPrizeAllocation(CardGameProduct cardGameProduct) {
        this.cardGameProductMapper.insert(cardGameProduct);
        return Result.success();
    }


    /**
     * 修改奖品
     * @param cardGameProduct
     * @return
     */
    @Override
    public Result upPrizeAllocation(CardGameProduct cardGameProduct) {
        cardGameProductMapper.updateById(cardGameProduct);
        return Result.success();
    }

    /**
     * 删除奖品
     * @param id
     * @return
     */
    @Override
    public Result delPrizeAllocation(Integer id) {
        this.cardGameProductMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 根据id查询活动奖品
     * @param id
     * @return
     */
    @Override
    public Result prizeAllocation(Integer id) {
        CardGameProduct cardGameProduct = this.cardGameProductMapper.selectById(id);
        return cardGameProduct==null?Result.noResult():Result.success(cardGameProduct);
    }

    /**
     * 活动策略列表
     * @return
     */
    @Override
    public Result getupStrategys() {
        List<CardGameRules> cardGameRules = cardGameRulesMapper.selectList(null);
        if(CollectionUtils.isEmpty(cardGameRules)){
            return Result.noResult();
        }

        return Result.success(cardGameRules);
    }

    /**
     * 添加活动策略
     * @param cardGameRules
     * @return
     */
    @Override
    public Result addupStrategy(CardGameRules cardGameRules) {
        cardGameRulesMapper.insert(cardGameRules);
        return Result.success();
    }

    /**
     * 修改活动策略
     * @param cardGameRules
     * @return
     */
    @Override
    public Result upStrategy(CardGameRules cardGameRules) {
        cardGameRulesMapper.updateById(cardGameRules);
        return Result.success();
    }

    /**
     * 删除活动策略
     * @param id
     * @return
     */
    @Override
    public Result delStrategy(Integer id) {
        cardGameRulesMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 根据id查询活动策略
     * @param id
     * @return
     */
    @Override
    public Result getStrategyId(Integer id) {
        CardGameRules cardGameRules = cardGameRulesMapper.selectById(id);
        return cardGameRules==null?Result.noResult():Result.success(cardGameRules);
    }
}
